// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Netcode replace addPublicVariableEventHandler by WC eventhandler
// -----------------------------------------------

// Recieve mission choosen by player
WC_fnc_netcode_server_wcaskformission = {
	private ["_count","_missionnumber","_player","_score","_wcaskformission"];
	_wcaskformission = _this select 0;
	if (isNil "wccurrentmission") then {
		_player        = _wcaskformission select 0;
		_missionnumber = _wcaskformission select 1;
		_score = score _player;
		_count = 0;
		{
			if (isPlayer _x) then {
				if (score _x > _score) then {
					_count = _count + 1;
				};
			};
			sleep 0.01;
		} foreach allUnits;
		if (_count < 4) then {
			{
				if (_x select 0 == _missionnumber) then {
					wccurrentmission = _x;
				};
				sleep 0.01;
			} foreach wclistofmissions;
		};
	};
};

// Promote a player
WC_fnc_netcode_server_wcpromote = {
	private ["_name","_rank","_wcpromote"];
	_wcpromote = _this select 0;
	(_wcpromote select 0) setRank (_wcpromote select 1);
	if (wckindofserver != 3) then {
		wcteamscore = wcteamscore + 1;
	};
	_name = name (_wcpromote select 0);
	_rank = _wcpromote select 1;
	diag_log format ["WARCONTEXT: %1 HAS BEEN PROMOTED TO %2 RANK !", _name, _rank];
};

// Degrade a player
WC_fnc_netcode_server_wcdegrade = {
	private ["_name","_rank","_wcdegrade"];
	_wcdegrade = _this select 0;
	(_wcdegrade select 0) setRank (_wcdegrade select 1);
	if (wckindofserver != 3) then {
		wcteamscore = wcteamscore - 1;
	};
	_name = name (_wcdegrade select 0);
	_rank = _wcdegrade select 1;
	diag_log format ["WARCONTEXT: %1 HAS BEEN DEGRADED TO %2 RANK !", _name, _rank];
};

// A player respawn to tent
WC_fnc_netcode_server_wcrespawntotent = {
	private ["_respawntotent"];
	if (wckindofserver != 3) then {
		_respawntotent = _this select 0;
		if (_respawntotent in wcinteam) then {
			wcteamscore = wcteamscore - 1;
		};
	};
};

// A player respawn to hq
WC_fnc_netcode_server_wcrespawntohq = {
	private ["_respawntohq"];
	if (wckindofserver != 3) then {
		_respawntohq = _this select 0;
		if (_respawntohq in wcinteam) then {
			wcteamscore = wcteamscore - 1;
		};
	};
};

// A player respawn to base
WC_fnc_netcode_server_wcrespawntobase = {
	private ["_respawntobase"];
	if (wckindofserver != 3) then {
		_respawntobase = _this select 0;
		if (_respawntobase in wcinteam) then {
			wcteamscore = wcteamscore - 1;
		};
	};
};

// Add player in ready state
WC_fnc_netcode_server_wcplayerreadyadd = {
	wcplayerreadyadd = _this select 0;
	wcplayerready = wcplayerready + [wcplayerreadyadd];
};

// Add x points to teamscore
WC_fnc_netcode_server_wcaddscore = {
	wcteamscore = wcteamscore + (_this select 0);
};

// Add x points to team bonus score
WC_fnc_netcode_server_wcteambonusaddscore = {
	wcteambonus = wcteambonus + (_this select 0);
};

// Add x points to player
WC_fnc_netcode_server_wcplayeraddscore = {
	private ["_index","_find","_player","_point","_score","_playername","_wcplayeraddscore"];
	_wcplayeraddscore = _this select 0;

	_player = _wcplayeraddscore select 0;
	_point  = _wcplayeraddscore select 1;
	_playername = name _player;

	_index = 0;
	_find = false;

	{
		if ((_x select 0) == _playername) then {
			_score = (_x select 2) + _point;
			if (_score < 0) then {_score = 0};
			wcscoreboard set [_index, [_playername, _player, _score]];
			_find = true;
		};
		_index = _index + 1;
		sleep 0.01;
	} foreach wcscoreboard;

	if !(_find) then {
		if (_point < 0) then {
			wcscoreboard = wcscoreboard + [[_playername, _player, 0]];
		} else {
			wcscoreboard = wcscoreboard + [[_playername, _player, _point]];
		};
	};
};

// Increase the detection level of x prct
WC_fnc_netcode_server_wcalerttoadd = {
	wcalert = wcalert + (_this select 0);
};

// Log blame
WC_fnc_netcode_server_wctk = {
	diag_log format ["WARCONTEXT: %1 IS TEAM KILLER", _this select 0];
};

//A3 TODO: Redo
// Unflip a vehicle
WC_fnc_netcode_server_wcflip = {
	private ["_vehicle","_position","_typeof","_dir","_dammage","_vehiclename","_objets_charges","_fuel"];
	_vehicle = _this select 0;
	if (!(locked _vehicle) and (getDammage _vehicle < 0.9)) then {
		_typeof = typeOf _vehicle;
		_position = (getPosATL _vehicle);
		_dir = getDir _vehicle;
		_dammage = getDammage _vehicle;
		_fuel = fuel _vehicle;
		_vehiclename = vehicleVarName _vehicle;
		_objets_charges = _vehicle getVariable "R3F_LOG_objets_charges";
		deleteVehicle _vehicle;
		_vehicle = _typeof createVehicle _position;
		_vehicle setVehicleVarName _vehiclename;
		//_vehicle setVehicleInit format ["this setVehicleVarName '%1';", _vehiclename];
		//processInitCommands;
		_vehicle setPosATL _position; 
		_vehicle setDir _dir;
		_vehicle setDamage _dammage;
		_vehicle setFuel _fuel;
		_vehicle setVariable ["R3F_LOG_objets_charges", _objets_charges, true];
	};
};

//A3 TODO: setVehicleInit
// Admin command - unlock all vehicles
WC_fnc_netcode_server_wcunlockall = {
	{
		_x setVehicleInit "this lock false;";
	} foreach vehicles;
	processInitCommands;
};

// Admin command - lock all vehicles
WC_fnc_netcode_server_wclockall = {
	{
		_x setVehicleInit "this lock true;";
	} foreach vehicles;
	processInitCommands;
};

// Bomb via c130 request by admin on a zone
WC_fnc_netcode_server_wcbombingrequest = {
	if (wcbombingavalaible == 1) then {
		diag_log "WARCONTEXT: CALL 1 C130 BOMBING SUPPORT";
		wcgarbage = [] spawn WC_fnc_bomb;
		wcbombingavalaible = 0;
		["wcbombingavalaible", "client"] call WC_fnc_publicvariable;
	};
};

// Count how many W soldier died during a mission, and complete campaign
WC_fnc_netcode_server_wcaddkilled = {
	wcaddkilled = _this select 0;
	wcnumberofkilled = wcnumberofkilled + 1;
	wcnumberofkilledofmissionW = wcnumberofkilledofmissionW + 1;
	wcgrave = wcgrave + 1;
	if ((name wcaddkilled) in wcinteam) then {
		if (wckindofserver != 3) then {
			wcteamscore = wcteamscore - 1;
		};
	};
};

// Recompute the list of missions when admin asks
WC_fnc_netcode_server_wcrecomputemission = {
	wcday = wcday + 1;
	wclistofmissions = [];
	wcgarbage = [] spawn WC_fnc_createlistofmissions;
};

// Insert player name died during a one life mission
WC_fnc_netcode_server_wctoonelife = {
	if !((_this select 0) in wconelife) then {
		wconelife = wconelife + [(_this select 0)];		
	};
};

// Start a defend mission
WC_fnc_netcode_server_wcbegindefend = {
	wcbegindefend = _this select 0;
};

// Cancel a mission by admin
WC_fnc_netcode_server_wcmissionsuccess = {
	wcmissionsuccess = _this select 0;
};

// Retrieve team members
WC_fnc_netcode_server_wcinteam = {
	wcinteam = _this select 0;
};